import time
from multiprocessing import Process, Pool
from random import random
from time import sleep
import os

# m = 1  # 对于全局变量访问，在每个进程都会拥有一个独立m变量，且互不影响
#
#
# def task1(second, name):  # 创建任务，可传入参数
#     global m
#     while True:
#         m += 1
#         sleep(second)
#         print('任务1.....', m, '进程', name, '进程号', os.getpid(), '进程1的父进程号', os.getppid())  # 利用os的进程号和父进程号
#
#
# def task2(second, name):
#     global m
#     while True:
#         m += 1
#         sleep(second)
#         print('任务2.....', m, '进程', name, '进程号', os.getpid(), '进程1的父进程号', os.getppid())
#
#
# number = 1
# if __name__ == '__main__':
#     p1 = Process(target=task1, name='任务1', args=(1, 'aa'))  # 将任务传入process，设置process的name属性为任务1,可以迭代器的形式传入参数args给任务task1
#     # p.run()  # 执行任务
#     p1.start()  # 使用Process的start方法创建进程
#     print(p1.name)  # 输出p的name属性
#     p2 = Process(target=task2, name='任务2', args=(2, 'bb'))
#     p2.start()
#     while True:
#         number += 1
#         sleep(0.2)
#         if number == 20:
#             p1.terminate()  # 终止进程
#             p2.terminate()
#             break
#         else:
#             print('number----------', number)


# # 自定义进程
# class MyProcess(Process):
#
#     def __init__(self, name):
#         super(MyProcess, self).__init__()
#         self.name = name
#
#     # 重写run方法
#     def run(self):
#         n = 1
#         while True:
#             print('{}------进程名{}'.format(n, self.name))
#             n += 1
#
#
# if __name__ == '__main__':
#     p1 = MyProcess(name='小明')
#     p1.start()
#
#     p2 = MyProcess(name='小红')
#     p2.start()


'''
非阻塞式进程，将所有任务加到队列里，完成一个，进入一个，同时执行几个
'''

# def task(task_name):
#     print('开始', task_name)
#     start = time.time()
#     time.sleep(random() * 2)
#     end = time.time()
#     # print('完成',task_name, '用时：',(end - start),'进程号：',os.getpid())
#     return '完成{}，用时：{}，进程号：{}'.format(task_name, (end - start), os.getpid())
#
#
# container = []
#
#
# # 回调函数，将task扔出的接收
# def callback_fun(n):
#     container.append(n)
#
#
# if __name__ == '__main__':
#     pool = Pool(5)  # 创建容量为5的进程池，依赖于主进程
#     tasks = ['听音乐', '看电视', '玩游戏', '做饭', '吃饭', '上厕所', '睡觉']
#     for i in tasks:
#         pool.apply_async(task, args=(i,), callback=callback_fun)  # 创建非阻塞式的进程，任务task，task名用列表传入，callback接收task的扔出结果
#
#     pool.close()  # 添加任务结束
#     pool.join()  # 将子进程插入主进程
#     for i in container:
#         print(i)
#     print('over-----')


'''
阻塞式进程和for循环相似，添加一个，执行一个，不可同时执行
'''

# def task(task_name):
#     print('开始', task_name)
#     start = time.time()
#     time.sleep(random() * 2)
#     end = time.time()
#     print('完成', task_name, '用时：', (end - start), '进程号：', os.getpid())
#
#
# if __name__ == '__main__':
#     pool = Pool(5)  # 创建容量为5的进程池，依赖于主进程
#     tasks = ['听音乐', '看电视', '玩游戏', '做饭', '吃饭', '上厕所', '睡觉']
#     for i in tasks:
#         pool.apply(task, args=(i,))  # 创建阻塞式的进程，任务task，task名用列表传入，不需要回调
#
#     pool.close()  # 添加任务结束
#     pool.join()  # 将子进程插入主进程
#
#     print('over-----')


from multiprocessing import Queue  # 队列

#
# q = Queue(5)
# q.put('A')  # 入队，队满可阻塞，put_nowait()非阻塞入队
# q.put('B')
# q.put('C')
# q.put('D')
# q.put('E')
# print(q.qsize())  # 队列大小
# if not q.full():  # 队列是否满
#     q.put('F', timeout=3)  # 等待3秒结束
# else:
#     print('队列已满')
#
# print(q.get())  # 出队，队空可阻塞，get_nowait()非阻塞出队
# print(q.get())
# print(q.get())
# print(q.get())
# print(q.get())
# if q.empty():
#     print('队列已空')
# else:
#     print(q.get())

'''
进程间通信

'''


# def downlod(q):
#     images = ['girl.jpg', 'boy.jpg', 'man.jpg']
#     for images in images:
#         print('正在下载', images)
#         sleep(0.5)
#         q.put(images)
#
#
# def getfile(q):
#     while not q.empty():
#         file = q.get()
#         print('{}保存成功'.format(file))
#
#
# if __name__ == '__main__':
#     q = Queue(5)
#     p1 = Process(target=downlod, args=(q,))  # 共用一个队列进行通信
#     p2 = Process(target=getfile, args=(q,))
#
#     p1.start()
#     p1.join()
#     p2.start()
#     p2.join()
